<?php
class UserInfo extends ModelBase
{
	private $tableName = 'myadmin_user';
	
	public static function getInstance()
	{
		return parent::instance(__CLASS__);
	}
	
	/**
	 * 获取用户信息
	 * @param bigInt $uid
	 * @return array
	 */
	public function getUserInfo($uid)
	{
		$user = Yii::app()->db->createCommand()
		->select('a.*,b.group_name,b.group_role')
		->from($this->tableName.' a')
		->leftJoin('myadmin_user_group b', 'b.group_id = a.user_group')
		->where('a.user_id=:uid', array(':uid'=>$uid))
		->queryRow();
		return $user;
	}
	
	public function getUserInfoByUserName($userName)
	{
		$user = Yii::app()->db->createCommand()
		->select('a.*,b.group_name,b.group_role')
		->from($this->tableName.' a')
		->leftJoin('myadmin_user_group b', 'b.group_id = a.user_group')
		->where('a.user_name=:name', array(':name'=>$userName))
		->queryRow();
		return $user;
	}
	
	
	public function getUsers($userGroup,$username,$offset=0,$pageSize = 10)
	{		
		$where = ' 1 ';
		if ($userGroup) $where .=' and user_group = '.$userGroup;
		if ($username) $where .=' and user_name = "'.$username.'"';
		$user = Yii::app()->db->createCommand()
		->select('b.*,a.group_name')
		->from($this->tableName." b ")
		->leftJoin('myadmin_user_group a', 'a.group_id = b.user_group')
		->where($where)
		->limit($pageSize,$offset)
		->queryAll();
		return $user;
	}
	
	
	public function getUsersCount($userGroup,$username)
	{
		$where = ' 1 ';
		if ($userGroup) $where .=' and user_group = '.$userGroup;
		if ($username) $where .=' and user_name = "'.$username.'"';
		$user = Yii::app()->db->createCommand()
		->select('count(*) as num')
		->from($this->tableName)
		->where($where)
		->queryRow();
		return $user;
	
	}
	
	
	public function delUser($uid)
	{
		if(!$uid)
		{
			return false;
		}
		$ret = Yii::app()->db->createCommand()->delete($this->tableName,'user_id = '.$uid);
		return $ret;
	}
	
	
	public function insertUserInfo($data)
	{
		$ret = Yii::app()->db->createCommand()->insert($this->tableName,$data);
		return $ret; 
	}
	
	public function updateUserInfo($data,$uid)
	{
		$ret = Yii::app()->db->createCommand()->update($this->tableName, $data,'user_id = '.$uid);
		return $ret;
	}
	
	public function changeUserGroup($ids,$group_id)
	{
		$ret = Yii::app()->db->createCommand()->update($this->tableName, array('user_group'=>$group_id),
				array('in', 'user_id', $ids));
		return $ret;
	}
	
	public function getMysqlVersion()
	{
		$versrion = Yii::app()->db->createCommand()
		->select('version() as version')
		->from($this->tableName)
		->queryRow();
		return $versrion['version'];
	}
	
}